#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        int target = sum - x;

        if (target < 0)
            return -1;
        int left = 0, right = 0;
        int n = -1;
        int Sum = 0;
        while (right < nums.size()) {
            Sum += nums[right++];

            while (Sum > target) {

                Sum -= nums[left++];
            }
            if (Sum == target)
                n = max(n, right - left);
        }
        if (n == -1)
            return n;
        return nums.size() - n;
    }
};